package com.informationsystem.webapp.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;

import com.informationsystem.model.Attachement;
import com.informationsystem.model.Downloads;
import com.informationsystem.service.AttachementManager;
import com.informationsystem.service.DownloadsManager;

public class DownloadsFileAction extends BaseAction {
	private InputStream download;
	private DownloadsManager downloadsManager;
	private Long downloadsid;
	private String filename;
	private String realName;
	private String contentType;

	public InputStream getDownload() {
		return this.download;
	}

	@Override
	public String execute() throws Exception {
		Downloads d = downloadsManager.get(downloadsid);
		if(d==null)
			return ERROR;
		File file = new File(ServletActionContext.getServletContext().getRealPath("/upload")+"\\"+
												"downloads_"+d.getId() + "_" + d.getFilename());
		if(file.exists())
			this.filename = d.getFilename();
		else
			return ERROR;
		InputStream in = new FileInputStream(file);
		setDownload(in); 
		return SUCCESS;
	}

	public String getFilename() {
		String downFileName = filename;   
        try {   
            downFileName = new String (filename.getBytes(), "ISO8859-1");   
        } catch (UnsupportedEncodingException e) {   
            e.printStackTrace();   
        }   
  
        return downFileName;   
	}

	public void setFilename(String filename) {		
		this.filename = filename;
	}

	public void setDownload(InputStream download) {
		this.download = download;
	}


	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public DownloadsManager getDownloadsManager() {
		return downloadsManager;
	}

	public void setDownloadsManager(DownloadsManager downloadsManager) {
		this.downloadsManager = downloadsManager;
	}

	public Long getDownloadsid() {
		return downloadsid;
	}

	public void setDownloadsid(Long downloadsid) {
		this.downloadsid = downloadsid;
	}
}
